require(['./config'],() => {
	require(['template','header','greycar','footer'],(template,header) =>{
		
		class Shopping{
			constructor() {
			   this.render() 
			   this.calcTotalMoney()
			   this.setAllCheckStatus()
			   this.allcheckChange()
			   this.checksChange()
			   this.numreduce()
			   this.numadd()
			   this.delete()
			   this.back ()
			    header.calcarcount()
				
			}
			
			render(){
			const cart = localStorage.getItem('cart')
			
			if(cart){
				$('.shoppingtrolley').show()
				$('.nocake').hide()
				this.cart = JSON.parse(cart)
				 $('.shoppingtrolley_center').html(template('shoppingcar_template', { cart: this.cart }))
			}else{
				// 没有商品加入购物车,购物车隐藏,加点东西
				$('.shoppingtrolley').hide()
				$('.nocake').show()
			}
		  }
		  checksChange (){
			  let _this = this
			  $('.check').on('change',function(){
				let id = $(this).parents('ul').data('id')
				  console.log(id)
				  _this.cart = _this.cart.map(shop => {
				    if (shop.id === id) shop.check = $(this).prop('checked')
				    return shop
				  })
				  localStorage.setItem('cart', JSON.stringify(_this.cart))
				  _this.setAllCheckStatus()
				  _this.calcTotalMoney()
				  
			  })
		  }
		  allcheckChange (){
			  let _this = this
			
			  $('.checks').on('change',function(){ 
				  // $('.check').prop('checked') = $(this).prop('checked')
				   console.log($(this).prop('checked'))
				  _this.cart = _this.cart.map(shop => {
					   shop.check = $(this).prop('checked')
					  return shop
				  })
				  localStorage.setItem('cart', JSON.stringify(_this.cart))
				  _this.setAllCheckStatus()
				  _this.calcTotalMoney()
				   _this.cchange()
			  })
		  }
		  cchange(){
			  const isCheck = this.cart.every(shop => shop.check)
			  $('.check').prop('checked', isCheck)
			  
		  }
		  numreduce(){
			  let _this = this
			  $('.right_cake_reduce').on('click',function(){
				let id = $(this).parents('ul').data('id') 
				 _this.cart = _this.cart.map(shop => {
					 if(shop.id === id) {
						 if(shop.count > 0){
							 
						 shop.count --
						 console.log(shop.count)
						 $(this).next().val(shop.count)
						 $(this).parent().next().html((shop.count *shop.num).toFixed(2))
						 }
					}
					 // $(this).next().val(shop.count)
					// console.log(shop.count)
					return shop
				 })
				 // console.log(_this.cart)
				 // $(this).next().val(_this.cart[count])
				 localStorage.setItem('cart', JSON.stringify(_this.cart))
				 // console.log(_this.cart[count])
				 header.calcarcount()
				_this.calcTotalMoney()
			  })
		  }
		  numadd(){
		  			  let _this = this
					  
		  			  $('.right_cake_add').on('click',function(){
		  				let id = $(this).parents('ul').data('id') 
		  				 
		  				 _this.cart = _this.cart.map(shop => {
		  					 if(shop.id === id) {
								 
									 shop.count ++
									 console.log(shop.count)
								 $(this).prev().val(shop.count)
								 $(this).parent().next().html((shop.count *shop.num).toFixed(2)) 							
								 }

							 return shop
		  				 })
		  				 localStorage.setItem('cart', JSON.stringify(_this.cart))
		  				 header.calcarcount()
							 
		  				_this.calcTotalMoney()
		  			  })
		  }
		  // inputvalue(){
			 //  const isvalue = this.cart.every(shop => shop.value)
			 //  $('.check').prop('checked', isCheck)
		  // }
		  delete(){
			  const _this = this
			  $('.cha').on('click',function () {
				  console.log(_this.cart)
				  const id = $(this).parent('ul').data(id)
				  console.log(id)
				  _this.cart = _this.cart.filter(shop => shop.id !== id.id)
				  
				  localStorage.setItem('cart', JSON.stringify(_this.cart))
			      console.log(_this.cart)
				  $(this).parent('ul').remove()
				  _this.setAllCheckStatus()
				  _this.calcTotalMoney()
				  
				  console.log(_this.cart[0])
				  if((_this.cart[0]) === undefined){
					  console.log(2)
					  localStorage.removeItem('cart')
					 
					  $('.shoppingtrolley').hide()
					  $('.nocake').show()
				  }
				  header.calcarcount()
			  })
		  }
		  setAllCheckStatus () {
		    const isAllCheck = this.cart.every(shop => shop.check)
		    $('.checks').prop('checked', isAllCheck)
		  }
		   calcTotalMoney () {
			   const total = this.cart.reduce((money,shop) => {
				   if (shop.check) money += shop.num * shop.count
				   return money
			   },0)
			  $('#allmoney').html(total.toFixed(2)) 
		   }
		 back (){
			 $('.back').on('click',()=>{
				 location.href = '/html/modules/top.html'
			 })
		 }
		 
		}
		new Shopping()
	})
})
